﻿using MES_SystemUI.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MES_SystemUI.UI
{
    public partial class WorkShopsPutin : MES_SystemUI.UI.Putin
    {
        public WorkShopsPutin()
        {
            InitializeComponent();
            this.Load += WorkShopsPutin_Load;
            textBox2.Enabled = false;
            comboBox1.SelectionChangeCommitted += ComboBox1_SelectionChangeCommitted;
        }

        private void ComboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                using (var c = new Context.MES_SystemContext())
                {
                    textBox2.Text = c.users.Find(comboBox1.SelectedValue).Phone;
                }
            }

        }

        private void WorkShopsPutin_Load(object sender, EventArgs e)
        {
            ComboBoxBingData();
            RefreshText();
        }

        private void RefreshText()
        {
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            comboBox1.SelectedIndex = -1;
            comboBox1.Text = string.Empty;
            comboBox2.SelectedIndex = -1;
            comboBox2.Text = string.Empty;
        }

        private void ComboBoxBingData()
        {
            using (var c = new Context.MES_SystemContext())
            {
                comboBox1.DataSource = c.users.ToList();
                comboBox1.DisplayMember = "UserName";
                comboBox1.ValueMember = "UserId";
                comboBox2.DataSource = c.workShopCategories.ToList();
                comboBox2.DisplayMember = "WorkShopCategoryName";
                comboBox2.ValueMember = "WorkShopCategoryId";
            }
        }
        public override void PutinData()
        {
            if(comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("负责人不能为空");
                return;
            }
            if(comboBox2.SelectedIndex == -1)
            {
                MessageBox.Show("车间类型不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("车间名称不能为空");
                return;
            }
            using (var c = new Context.MES_SystemContext())
            {
                WorkShop mode = new WorkShop()
                {
                    WorkShopName = textBox1.Text,
                    WorkShopContactId = int.Parse(comboBox1.SelectedValue.ToString()),
                    WorkShopCategotyId = int.Parse(comboBox2.SelectedValue.ToString()),
                    CreateUserId = SystemDataConfig.LoginUser.UserId,
                };
                c.Entry(mode).State = System.Data.Entity.EntityState.Added;
                c.SaveChanges();
            }
            RefreshText();
            MessageBox.Show("提交成功！");
        }
    }
}
